﻿using Prism.Dialogs;
using Prism.Mvvm;
using System;
using System.Windows.Threading;

namespace YY.Admin.ViewModels.Dialogs
{
    public class ErrorDialogViewModel : BindableBase, IDialogAware
    {
        public string Title => "错误";

        private string _message;
        public string Message
        {
            get => _message;
            set => SetProperty(ref _message, value);
        }

        // 用属性来实现，而不是 event
        public DialogCloseListener RequestClose { get; private set; }

        public ErrorDialogViewModel()
        {
            // 自动关闭定时器（2秒）
            var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
            timer.Tick += (s, e) =>
            {
                timer.Stop();
                RequestClose.Invoke(new DialogResult(ButtonResult.OK));
            };
            timer.Start();
        }

        public bool CanCloseDialog() => true;

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Message = parameters.GetValue<string>("message");
        }

        public void OnDialogClosed()
        {
        }
    }
}
